<?php

class hAtomBlockController extends BlockController {

//	protected $btDescription = "hAtom Block";
//	protected $btName = "hAtom";
	protected $btTable = 'bthAtom';
	protected $btInterfaceWidth = "800";
	protected $btInterfaceHeight = "600";

	private $request = array();

	function __construct($object = null) {		
		parent::__construct($object);
		$this->db = Loader::db();
	}

	public function getJavaScriptStrings() {
		return array();
	}

	public function getBlockTypeDescription() {
		return t("hAtom Block)");
	}
	
	public function getBlockTypeName() {
		return t("hAtom");
	}

	public function on_page_view() {
		$this->getEntries();
		$this->set("entries", $this->entries);
	}

	public function getEntries() {
		Loader::model("page");
		Loader::model("userInfo");

		$sql = "SELECT * FROM bthAtomEntries WHERE bID=".intval($this->bID)." ORDER BY updated DESC";
		$entries = $this->db->getAll($sql);
		$entriesView = array();
		foreach ($entries as $key => $entry) {
			$page		= Page::getById(intval($entry["pid"]));
			$author	= UserInfo::getByID(intval($entry["author"]));
			if ($author) {
				$entry["authorName"]	= $author->getUserAuthorname();
				$entry["authorBlog"]		= $author->getUserAuthorblog();
			}
			if ($page) {
				$entry["pageName"]	= $page->getCollectionName();
				$entry["pagePath"]		= $page->getCollectionPath();
			}
			$entriesView[] = $entry;
		}
		$this->entries = $entriesView;
	}

	public function save($p) {
		$this->request = $p;
		$entries = $this->_getPostEntries();

		$this->db->query("DELETE FROM bthAtomEntries WHERE bID=".intval($this->bID));
		foreach ($entries as $key => $e) {
			$vals = array(intval($this->bID), $e["title"],	$e["published"], $e["updated"], $e["author"], $e["summary"], $e["content"], $e["pid"]);
			$this->db->query("INSERT INTO bthAtomEntries (bID,title,published,updated,author,summary,content,pid) values (?,?,?,?,?,?,?,?)", $vals);
		}
		$block = array("bID" => intval($this->bID), "hAtomTitle" => $p["hAtomTitle"]);
		parent::save($block);
	}

	private function _getPostEntries() {
		$rowIds		= $this->_getPostParameters("rowid");
		$title			= $this->_getPostParameters("title");
		$published	= $this->_getPostParameters("published");
		$updated		= $this->_getPostParameters("updated");
		$author		= $this->_getPostParameters("author");
		$summary	= $this->_getPostParameters("summary");
		$content		= $this->_getPostParameters("content");
		$pid				= $this->_getPostParameters("pid");

		foreach ($rowIds as $key => $value) {
			$entry = array(
				"bID" => intval($this->bID),
				"title" => $title[$key],
				"published" => $published[$key],
				"updated" =>$updated [$key],
				"author" => $author[$key],
				"summary" => $summary[$key],
				"content" => $content[$key],
				"pid" => $pid[$key],
			);
			$entries[] = $entry;
		}
		return $entries;
	}

	private function _getPostParameters($key) {
		$values = array();
		$parameters = $this->request[$key];
		foreach ($parameters as $key => $value) {
			$values[] = $value;
		}
		return $values;
	}

	function delete(){
		$this->db->query("DELETE FROM bthAtomEntries WHERE bID=".intval($this->bID));		
		parent::delete();
	}
}
	
?>